package com.minivision.sdktestdemo.ui.view;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.minivision.sdktestdemo.R;
import com.minivision.sdktestdemo.bean.MusicBean;
import com.minivision.sdktestdemo.event.DataModificationEvent;
import com.minivision.sdktestdemo.event.PlayMusicEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by devtang on 2016/5/24.
 */
public class MusicListPopContentView implements PopContentView, MusicListPopItemRecyclerAdapter.OnItemClickListener {
    private PopupWindow mPopupWindow;

    private Activity mContext;

    private View mContentView;

    @Bind(R.id.tv_list_clear)
    TextView mClearText;

    @Bind(R.id.tv_list_num)
    TextView mNumText;

    @Bind(R.id.pop_music_list)
    RecyclerView mRecylerView;

    MusicListPopItemRecyclerAdapter mAdapter;

    private AlertDialog mClearDialog;

    public MusicListPopContentView(Activity context) {
        mContext = context;
        ButterKnife.bind(this, getContentView());
        mAdapter = new MusicListPopItemRecyclerAdapter();
        mAdapter.setOnItemClickListener(this);
        mRecylerView.setLayoutManager(new LinearLayoutManager(context));
        mRecylerView.setHasFixedSize(true);
        mRecylerView.setAdapter(mAdapter);
        mClearText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setCustomTitle(LayoutInflater.from(mContext).inflate(R.layout.layout_title, null));
                builder.setPositiveButton("清空", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mClearDialog.dismiss();
                        mAdapter.getList().clear();
                        mAdapter.notifyDataSetChanged();
                        DataModificationEvent event = new DataModificationEvent(null, DataModificationEvent.ACTION_LIST_CLEAR);
                        EventBus.getDefault().post(event);
                        MusicListPopContentView.this.dismiss();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mClearDialog.dismiss();
                    }
                });
                mClearDialog = builder.create();
                mClearDialog.show();
            }
        });
    }

    @Override
    public int getMaxPopWindowHeight() {
        return getContext().getResources().getDisplayMetrics().heightPixels * 2 / 3;
    }

    @Override
    public View getContentView() {
        if (mContentView == null) {
            mContentView = mContext.getLayoutInflater().inflate(R.layout.pop_music_list_view, null);
        }
        return mContentView;
    }

    @Override
    public void dismiss() {
        if (mPopupWindow != null)
            mPopupWindow.dismiss();
    }

    @Override
    public void setPopupWindow(PopupWindow popupWindow) {
        mPopupWindow = popupWindow;
    }

    public void setMusicBeans(List<MusicBean> list) {
        mAdapter.setmBeans(list);
        mNumText.setText("(" + mAdapter.getList().size() + ")");
    }

    public void setMusicEvent(PlayMusicEvent event) {
        mAdapter.setmEvent(event);
    }

    public void notifyDataChanged() {
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public Activity getContext() {
        return mContext;
    }

    @Override
    public void onClickItem(int position) {
        int prev = mAdapter.getEventPosition(mAdapter.getEvent());
        mAdapter.getEvent().setPlayEvent(new PlayMusicEvent(mAdapter.getList().get(position), PlayMusicEvent.PLAY));
        mAdapter.notifyItemChanged(prev);
        mAdapter.notifyItemChanged(position);
        EventBus.getDefault().post(mAdapter.getEvent());
    }

    @Override
    public void onClickRemove(int position) {
        int prev = mAdapter.getEventPosition(mAdapter.getEvent());
        mAdapter.getList().remove(position);
        mAdapter.notifyDataSetChanged();
        if (position == prev) {
            if (mAdapter.getList().size() != 0) {
                position %= mAdapter.getList().size();
                mAdapter.getEvent().setPlayEvent(new PlayMusicEvent(mAdapter.getList().get(position), PlayMusicEvent.PLAY));
                EventBus.getDefault().post(mAdapter.getEvent());
            } else {
                DataModificationEvent event = new DataModificationEvent(null, DataModificationEvent.ACTION_LIST_CLEAR);
                dismiss();
                EventBus.getDefault().post(event);
            }
        }
        mNumText.setText("(" + mAdapter.getList().size() + ")");
    }

    @Override
    public void onClickLocate(int postion) {

    }
}
